package research.ui.editors.tableeditors.viscosity;

import research.domain.ViscosityValue;
import research.entity.Entity;
import research.ui.editors.tableeditors.EntityCellModifier;
import research.ui.editors.tableeditors.EntityLabelProvider;
import research.ui.editors.tableeditors.EntityTableEditor;

public class ViscosityCellModifier extends EntityCellModifier {

	public ViscosityCellModifier(EntityTableEditor viewer) {
		super(viewer);
	}
	
	public static double calcViscosity(ViscosityValue par) {
		try {
			return par.getE() * 100 / (par.getD() * par.getB());
		} catch (Exception e) {
			return Double.NaN;
		}
	}

	@Override
	protected Object getValue(Entity entity, int columnIndex) {
		ViscosityValue par = (ViscosityValue) entity;

		switch (columnIndex) {
		case 0:
			return EntityLabelProvider.render(par.getNumber());
		case 1:
			return EntityLabelProvider.render(par.getD());
		case 2:
			return EntityLabelProvider.render(par.getB());
		case 3:
			return EntityLabelProvider.render(par.getE());
		case 4:
			return EntityLabelProvider.render(calcViscosity(par));
		case 5:
			return EntityLabelProvider.render(par.getNote());
		default:
		}
		return null;
	}

	@Override
	protected void modify(Entity entity, int columnIndex, Object value) {
		ViscosityValue par = (ViscosityValue) entity;
		switch (columnIndex) {
		case 0:
			par.setNumber(parseInt(value));
			break;
		case 1:
			par.setD(parseDouble(value));
			break;
		case 2:
			par.setB(parseDouble(value));
			break;
		case 3:
			par.setE(parseDouble(value));
			break;
		case 4:
			par.setEn(parseDouble(value));
			break;
		case 5:
			par.setNote(parseString(value));
			break;
		default:
		}
	}
	
	@Override
	public boolean canModify(Object element, String property) {
		return getColumnIndex(property) != 4;
	}

}
